package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.pojo.Order;
import com.qf.pojo.User;
import com.qf.service.OrderService;
import com.qf.vo.OrderItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping(value = "/orderlist")
        public  PageInfo<OrderItem> findAll(HttpServletRequest req){
            String page = req.getParameter("page");
            int p=page==null?1:Integer.valueOf(page);
            System.out.println(p);
            PageInfo<OrderItem> alls2 = orderService.findAlls2( p, 3);
        System.out.println(alls2);
        return alls2;

        /*List<Order> all = orderService.findAll();
        return all;*/
    }

    @PostMapping(value = "/orderlist")
    public Order add(@RequestBody Order order, HttpSession session){
        if(order.getUid ()==0){
            Object user = session.getAttribute ("user");
            if (user!=null && user instanceof User){
                order.setUid (((User) user).getId ());
            }
        }
        Order order1 = orderService.addOrder(order);
        return order1;
    }

    @GetMapping(value = "/orders")
    public PageInfo<OrderItem> findUserOrder(HttpServletRequest req, HttpServletResponse resp){
        HttpSession session = req.getSession();
        Object user = session.getAttribute ("user");
        if (user!=null && user instanceof User){
            int uid = ((User) user).getId ();
            String page = req.getParameter("page");
            int p=page==null?1:Integer.valueOf(page);
            System.out.println(p);
            PageInfo<OrderItem> alls = orderService.findAlls(uid, p, 3);
            System.out.println(alls);

            return alls;
        }
        return null;
    }
    @PutMapping(value = "/order")
    public Order update(@RequestBody Order order){
        System.out.println(order);
        Order one = orderService.updateOrder(order);
        return order;
    }

    @DeleteMapping(value="/order{id}")
    public Boolean delete(@PathVariable("id") int id){
        Boolean aBoolean = orderService.deleteOrder(id);
        return aBoolean;
    }

    @DeleteMapping(value="/orderu{id}")
    public Boolean deleteu(@PathVariable("id") int id){
        Boolean aBoolean = orderService.deleteOrder(id);
        return aBoolean;
    }
}
